import Vue from 'vue'
import App from './App.vue'
import router from './router'
import api from './api' // 导入api接口
import './plugins/element.js'
import '@/filters/format-date'//时间过滤器
import '@/filters/hideName'//名称过滤器
import 'normalize.css'
import { Swipe, SwipeItem, PullRefresh ,List} from 'vant';
import { Toast, Dialog} from 'vant';


Vue.use(Swipe).use(SwipeItem).use(PullRefresh).use(List);
Vue.use(Toast).use(Dialog);
import VueAwesomeSwiper from 'vue-awesome-swiper'

// require styles
import 'swiper/dist/css/swiper.css'
Vue.prototype.$api = api; // 将api挂载到vue的原型上
Vue.use(VueAwesomeSwiper, /* { default global options } */)

Vue.config.productionTip = false


/**
 * 设置cookie
 * @param {type} name
 * @param {type} value
 * @returns {undefined}
 */

Vue.prototype.setCookie = function (name,value) {
  var Days = 30;
  var exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
}



/**
 * 系统随机参数
 * @param {type} len
 * @returns {String}
 */
Vue.prototype.randomString = function (len) {
  len = len || 32;
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  var maxPos = $chars.length;
  var pwd = '';
  for (var i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}

/**
 * 获取cookie
 * @param {type} name
 * @returns {unresolved}
 */

Vue.prototype.getCookie = function (name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if (arr = document.cookie.match(reg)) {
    return unescape(arr[2]);
  }
  return null;
}

/**
 * 删除cookie
 * @param {type} name
 * @returns {undefined}
 */


Vue.prototype.delCookie = function (name) {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = this.getCookie(name);
  if (cval !== null)
    document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
}
/**
 * 验证登录
 */
Vue.prototype.tologin = function (path) {

    if(this.getCookie("token")==null||this.getCookie("token")==''){
      router.push({
        path: '/login',
        query: {

        }
      });
    }else{
      router.push({
        path: path,
        query: {

        }
      });
    }
}


router.beforeEach((to, from, next) =>{
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
